package cn.iocoder.yudao.module.forward.service.group;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.forward.controller.admin.group.vo.*;
import cn.iocoder.yudao.module.forward.dal.dataobject.group.GroupDO;
import cn.iocoder.yudao.module.forward.dal.dataobject.group.GroupAclDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.forward.dal.mysql.group.GroupMapper;
import cn.iocoder.yudao.module.forward.dal.mysql.group.GroupAclMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.forward.enums.ErrorCodeConstants.*;

/**
 * 分组 Service 实现类
 *
 * @author zhang
 */
@Service
@Validated
public class GroupServiceImpl implements GroupService {

    @Resource
    private GroupMapper groupMapper;
    @Resource
    private GroupAclMapper groupAclMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer createGroup(GroupSaveReqVO createReqVO) {
        // 插入
        GroupDO group = BeanUtils.toBean(createReqVO, GroupDO.class);
        groupMapper.insert(group);

        // 插入子表
        createGroupAclList(group.getId(), createReqVO.getGroupAcls());
        // 返回
        return group.getId();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateGroup(GroupSaveReqVO updateReqVO) {
        // 校验存在
        validateGroupExists(updateReqVO.getId());
        // 更新
        GroupDO updateObj = BeanUtils.toBean(updateReqVO, GroupDO.class);
        groupMapper.updateById(updateObj);

        // 更新子表
        updateGroupAclList(updateReqVO.getId(), updateReqVO.getGroupAcls());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteGroup(Integer id) {
        // 校验存在
        validateGroupExists(id);
        // 删除
        groupMapper.deleteById(id);

        // 删除子表
        deleteGroupAclByGroupId(id);
    }

    private void validateGroupExists(Integer id) {
        if (groupMapper.selectById(id) == null) {
            throw exception(GROUP_NOT_EXISTS);
        }
    }

    @Override
    public GroupDO getGroup(Integer id) {
        return groupMapper.selectById(id);
    }

    @Override
    public PageResult<GroupDO> getGroupPage(GroupPageReqVO pageReqVO) {
        return groupMapper.selectPage(pageReqVO);
    }

    // ==================== 子表（分组权限） ====================

    @Override
    public List<GroupAclDO> getGroupAclListByGroupId(Integer groupId) {
        return groupAclMapper.selectListByGroupId(groupId);
    }

    @Override
    public List<GroupDO> getAllGroup(GroupPageReqVO pageReqVO) {
        return groupMapper.selectList("enable",1);
    }

    @Override
    public GroupDO getGroupByCode(String code) {
        return groupMapper.selectOne("code",code);
    }

    private void createGroupAclList(Integer groupId, List<GroupAclDO> list) {
        list.forEach(o -> o.setGroupId(groupId));
        groupAclMapper.insertBatch(list);
    }

    private void updateGroupAclList(Integer groupId, List<GroupAclDO> list) {
        deleteGroupAclByGroupId(groupId);
		list.forEach(o -> o.setId(null).setUpdater(null).setUpdateTime(null)); // 解决更新情况下：1）id 冲突；2）updateTime 不更新
        createGroupAclList(groupId, list);
    }

    private void deleteGroupAclByGroupId(Integer groupId) {
        groupAclMapper.deleteByGroupId(groupId);
    }

}